package com.us.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import net.jueb.util4j.cache.map.TimedMapImpl;

@Controller
public class LookUsController {

	private TimedMapImpl<Integer, String> total = new TimedMapImpl<Integer , String>();
	private TimedMapImpl<Integer, String> top = new TimedMapImpl<Integer , String>();
	private TimedMapImpl<Integer, String> paoliang = new TimedMapImpl<Integer , String>();
	private TimedMapImpl<Integer, String> id = new TimedMapImpl<Integer , String>();
	
	{
		Thread t=new Thread(){
			public void run() {
				Runnable cleanTask1=total.getCleanTask();
				Runnable cleanTask2=top.getCleanTask();
				Runnable cleanTask3=paoliang.getCleanTask();
				Runnable cleanTask4=id.getCleanTask();
				while(true)
				{
					try {
						cleanTask1.run();
						cleanTask2.run();
						cleanTask3.run();
						cleanTask4.run();
						Thread.sleep(TimeUnit.HOURS.toMillis(12));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		};
		t.setDaemon(true);
		t.setName("RoleAgentCleanThread");
		t.start();
	}
	
	@RequestMapping("/total")
	public void total(HttpServletResponse res , HttpServletRequest req){
		String appKey = "q6ztwoh36g";
		String page = req.getParameter("page");
		String result =total.get(Integer.parseInt(page));
		if(StringUtils.isEmpty(result)){
			String url = "http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=q6ztwoh36g&v=2&page="+page ;
			
			result = getInfo(url);
			total.put(Integer.parseInt(page), result);
		}
		
		System.out.println(result);
		try {
			res.getWriter().write(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/top")
	public void top(HttpServletResponse res , HttpServletRequest req){
		String appKey = "q6ztwoh36g";
		String result =top.get(1);
		if(StringUtils.isEmpty(result)){
			String url = "http://api.dataoke.com/index.php?r=Port/index&type=top100&appkey="+appKey+"&v=2";
			
			result = getInfo(url);
			top.put(1, result);
		}
		
		System.out.println(result);
		try {
			res.getWriter().write(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/paoliang")
	public void paoliang(HttpServletResponse res , HttpServletRequest req){
		String appKey = "q6ztwoh36g";
		String result =paoliang.get(1);
		if(StringUtils.isEmpty(result)){
			String url = "http://api.dataoke.com/index.php?r=Port/index&type=paoliang&appkey="+appKey+"&v=2";
			
			result = getInfo(url);
			paoliang.put(1, result);
		}
		
		System.out.println(result);
		try {
			res.getWriter().write(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/id")
	public void id(HttpServletResponse res , HttpServletRequest req){
		String appKey = "q6ztwoh36g";
		String ids = req.getParameter("id");
		String result =id.get(Integer.parseInt(ids));
		if(StringUtils.isEmpty(result)){
			String url = "http://api.dataoke.com/index.php?r=port/index&appkey="+appKey+"&v=2&id="+ids;
			
			result = getInfo(url);
			id.put(Integer.parseInt(ids), result);
		}
		
		System.out.println(result);
		try {
			res.getWriter().write(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String getInfo(String url) {
		OutputStream outputStream = null;
		OutputStreamWriter outputStreamWriter = null;
		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		StringBuffer resultBuffer = new StringBuffer();
		String tempLine = null;
		try {

			URL localURL = new URL(url);
			URLConnection connection = localURL.openConnection();
			HttpURLConnection httpURLConnection = (HttpURLConnection) connection;

			httpURLConnection.setDoOutput(true);
			httpURLConnection.setRequestMethod("GET");
			httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
			httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

			connection.setConnectTimeout(120000);
			if (httpURLConnection.getResponseCode() >= 300) {

			} else {
				inputStream = httpURLConnection.getInputStream();
				inputStreamReader = new InputStreamReader(inputStream, "utf-8");
				reader = new BufferedReader(inputStreamReader);

				while ((tempLine = reader.readLine()) != null) {
					resultBuffer.append(tempLine);
				}

				String backstr = resultBuffer.toString();

				return backstr;

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally {
			try {
				if (outputStreamWriter != null) {

					outputStreamWriter.close();

				}

				if (outputStream != null) {
					outputStream.close();
				}

				if (reader != null) {
					reader.close();
				}

				if (inputStreamReader != null) {
					inputStreamReader.close();
				}

				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return "";
	}
	
}
